#include <QtGui/QPixmap>
#include <QtGui/QPainter>
#include <QtGui/QColorDialog>
#include "ColorButton.h"

ColorButton::ColorButton(const QColor& defaultColor,QWidget *par) {
	parent = par;
	setFixedSize(sizeHint());
	currentColor = defaultColor;
	setButtonColor(currentColor);
	setFixedSize(sizeHint());
	connect(this,SIGNAL(clicked()),this,SLOT(changeColor()));
}

const QColor& ColorButton::color() const {
	return currentColor;
}

void ColorButton::changeColor() {
	QColor chosenColor = QColorDialog::getColor(currentColor,parent);
	if (!chosenColor.isValid())
		return;
	currentColor = chosenColor;
	setButtonColor(currentColor);
}

void ColorButton::setButtonColor(const QColor &color) {
	if (color != currentColor)
		currentColor = color;
	
	QPixmap pixmap(width() - 10,height() - 10);
	QPainter painter(&pixmap);
	painter.setBrush(currentColor);
	painter.setPen(QPen(QColor(200, 200, 200)));
	painter.drawRect(0, 0, pixmap.width(),pixmap.height());
	painter.setPen(QPen(QColor(50, 50, 50)));
	painter.drawLine(0, 0, pixmap.width(), 0);
	painter.drawLine(0, 0, 0, pixmap.height());
	
	setIconSize(pixmap.size());
	setIcon(QIcon(pixmap));
}
